﻿


CREATE PROCEDURE [dbo].[fares_StergeUnitate] 
 @CodUnitate smallint AS
BEGIN
DECLARE @NrDoc AS int
SET @NrDoc = 0

BEGIN TRAN

PRINT('DocumentePlataDetaliu')
DELETE FROM [DocumentePlataDetaliu] WHERE IDDocumentPlata IN (SELECT IDDocumentPlata FROM [DocumentePlata] WHERE  [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('DocumentePlata')
DELETE FROM [DocumentePlata] WHERE  [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 



PRINT('DocumenteDetaliu')
DELETE FROM [DocumenteDetaliu] WHERE IDDocument IN (SELECT IDDocument FROM [Documente] WHERE  [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('[ArticoleParteneri]')
DELETE FROM [ArticoleParteneri] WHERE IDArticol IN (SELECT IDArticol FROM [articole] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 


PRINT('[ArticoleVariante]')
DELETE FROM [ArticoleVariante] WHERE IDArticol IN (SELECT IDArticol FROM [articole] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('[UMConversie]')
DELETE FROM [UMConversie] WHERE IDArticol IN (SELECT IDArticol FROM [articole] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 


PRINT('Articole')
DELETE FROM [articole] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT('Documente')
DELETE FROM [Documente] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare
     
PRINT('ArticoleParteneri')
DELETE FROM [ArticoleParteneri] WHERE IDPartener IN (SELECT IDPartener FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT('[ParteneriConturiBancare]')
DELETE FROM [ParteneriConturiBancare] WHERE IDPartener IN (SELECT IDPartener FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('[Contact]')
DELETE FROM [Contact] WHERE IDPartener IN (SELECT IDPartener FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('[Adrese]')
DELETE FROM [Adrese] WHERE IDPartener IN (SELECT IDPartener FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('[MijloaceTransport]')
DELETE FROM [MijloaceTransport] WHERE IDPartener IN (SELECT IDPartener FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT('Furnizori')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Furnizori]') AND type in (N'U'))
DELETE FROM [Furnizori] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT('fares_DocumenteImportate')
DELETE FROM fares_DocumenteImportate WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

    
PRINT('Parteneri')
DELETE FROM [Parteneri] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

DELETE FROM [Stocuri] WHERE IDArticol in (SELECT IDArticol FROM [articole] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 



PRINT 'FaresSetup'
DELETE FROM [FaresSetup] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'Trasee'
DELETE FROM [Trasee] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'ValoriDimensiuni'
DELETE FROM [ValoriDimensiuni] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


COMMIT TRAN    
GOTO Gata
Eroare:
  ROLLBACK
  RAISERROR ('Eroare la executia procedurii StergeUnitate',-1,0)
Exista_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine documente!',-1,0)
Exista_Rulaje_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine rulaje - documente!',-1,0)

Gata:

END